﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Edu.Web.Data.DV;
using Edu.Web.Data.Entity;

namespace Edu.Web.Data.DAO.Impl
{
    public partial class AttendanceDAO
    {
        public IQueryable<AttendanceGroupDV> GetAttendanceListByCreateUid(long uid)
        {
            return from attend in Db.Set<AttendanceGroupEntity>()
                   join clazz in Db.Set<ClazzEntity>() on attend.ClazzId equals clazz.Id
                   join u in Db.Set<UserEntity>() on attend.CreateUserId equals u.Id
                   where u.Id == uid
                   orderby attend.CreateTime descending
                   select new AttendanceGroupDV
                   {
                       ClazzId = clazz.Id,
                       ClazzName = clazz.ClazzName,
                       GroupName = attend.GroupName,
                       Id = attend.Id,
                       CreateTime = attend.CreateTime
                   };
        }

        public AttendanceGroupDV GetAttendanceGroupById(long id)
        {
            var query = from attend in Db.Set<AttendanceGroupEntity>()
                        join clazz in Db.Set<ClazzEntity>() on attend.ClazzId equals clazz.Id
                        join u in Db.Set<UserEntity>() on attend.CreateUserId equals u.Id
                        where attend.Id == id
                        orderby attend.CreateTime descending
                        select new AttendanceGroupDV
                        {
                            ClazzId = clazz.Id,
                            ClazzName = clazz.ClazzName,
                            GroupName = attend.GroupName,
                            Id = attend.Id,
                            CreateTime = attend.CreateTime
                        };

            return query.FirstOrDefault();
        }

        public IQueryable<AttendanceDV> GetAttendanceListByGroupId(long groupId)
        {
            return from attend in Db.Set<AttendanceEntity>()
                   join stu in Db.Set<StudentEntity>() on attend.StudentUserId equals stu.UserId
                   join u in Db.Set<UserEntity>() on stu.UserId equals u.Id
                   where attend.AttendanceGroupId == groupId
                   orderby stu.Id ascending
                   select new AttendanceDV
                   {
                       StudentNo = stu.StuNo,
                       Status = attend.Status,
                       StudentId = stu.Id,
                       StudentName = u.RealName
                   };
        }
    }
}